package com.allen.service;

import com.allen.bean.Like;
import com.allen.bean.Result;
import com.allen.mapper.LikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: allen
 * @Data: 2021.06.23.17:10
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class LikeService {

    @Autowired
    private LikeMapper likeMapper;      //注入mapper依赖

    public Result sel(Like like){
        Result result = new Result();       //实例化对象
        result.setSuccess(false);           //给变量赋值
        result.setDetail(null);
//        System.out.println(like.getW_username());
        Like exitid=likeMapper.findIdByName(like.getW_username(),like.getW_font(),like.getC_username());
        Boolean flag=likeMapper.findLikeByName(like);
        if(exitid!=null){       //此用户点过赞
            System.out.println("用户存在");
            if(flag==false){            //false：允许点赞
                Integer suc=likeMapper.updataLiket(like);
                if(suc==1){
                    result.setMsg("点赞成功");
                    likeMapper.appendlike(like);
                    result.setSuccess(true);
                }else{
                    result.setMsg("点赞失败");
                    result.setSuccess(false);
                }
            }else{
                result.setMsg("已点赞");
                result.setSuccess(false);
            }
        }else{
            Integer add=likeMapper.addLike(like);
            if(add==1){
                Integer suc=likeMapper.updataLiket(like);
                if(suc==1){
                    result.setMsg("点赞成功");
                    likeMapper.appendlike(like);
                    result.setSuccess(true);
                }else{
                    result.setMsg("点赞失败");
                    result.setSuccess(false);
                }
            }else{
                result.setMsg("点赞失败");
                result.setSuccess(true);
            }
        }
        return result;
    }

}
